package com.boc.ewr.outsrc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boc.ewr.outsrc.entity.OutsrcNdrDtl;
import com.boc.ewr.outsrc.service.IOutsrcNdrDtlService;
import com.boc.ewr.util.file.enumeration.FdbkStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.Set;

/**
 * @Description: 退信明细（待确认信息清单）
 * @Author: jeecg-boot
 * @Date: 2023-04-20
 * @Version: V1.0
 */
@Api(tags = "退信明细（待确认信息清单）")
@RestController
@RequestMapping("/outsrc/outsrcNdrDtl")
@Slf4j
public class OutsrcNdrDtlController extends JeecgController<OutsrcNdrDtl, IOutsrcNdrDtlService> {
    @Autowired
    private IOutsrcNdrDtlService outsrcNdrDtlService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    /**
     * 分页列表查询
     *
     * @param outsrcNdrDtl
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "退信明细（待确认信息清单）-分页列表查询")
    @ApiOperation(value = "退信明细（待确认信息清单）-分页列表查询", notes = "退信明细（待确认信息清单）-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<OutsrcNdrDtl>> queryPageList(OutsrcNdrDtl outsrcNdrDtl,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<OutsrcNdrDtl> queryWrapper = QueryGenerator.initQueryWrapper(outsrcNdrDtl, req.getParameterMap());
		Set<String> orgCodeSetByLoginUser = sysBaseAPI.getOrgCodeSetByLoginUser(this.getLoginUser());
		if (orgCodeSetByLoginUser!=null) {
			queryWrapper.in("org_no", orgCodeSetByLoginUser);
		}
		Page<OutsrcNdrDtl> page = new Page<OutsrcNdrDtl>(pageNo, pageSize);
        IPage<OutsrcNdrDtl> pageList = outsrcNdrDtlService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param outsrcNdrDtl
     * @return
     */
    @AutoLog(value = "退信明细（待确认信息清单）-添加")
    @ApiOperation(value = "退信明细（待确认信息清单）-添加", notes = "退信明细（待确认信息清单）-添加")
    //@RequiresPermissions("com.boc.ewr:outsrc_ndr_dtl:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody OutsrcNdrDtl outsrcNdrDtl) {
        outsrcNdrDtlService.save(outsrcNdrDtl);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param outsrcNdrDtl
     * @return
     */
    @AutoLog(value = "退信明细（待确认信息清单）-编辑")
    @ApiOperation(value = "退信明细（待确认信息清单）-编辑", notes = "退信明细（待确认信息清单）-编辑")
    //@RequiresPermissions("com.boc.ewr:outsrc_ndr_dtl:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody OutsrcNdrDtl outsrcNdrDtl) {
        outsrcNdrDtlService.updateById(outsrcNdrDtl);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "退信明细（待确认信息清单）-通过id删除")
    @ApiOperation(value = "退信明细（待确认信息清单）-通过id删除", notes = "退信明细（待确认信息清单）-通过id删除")
    //@RequiresPermissions("com.boc.ewr:outsrc_ndr_dtl:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        outsrcNdrDtlService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "退信明细（待确认信息清单）-批量删除")
    @ApiOperation(value = "退信明细（待确认信息清单）-批量删除", notes = "退信明细（待确认信息清单）-批量删除")
    //@RequiresPermissions("com.boc.ewr:outsrc_ndr_dtl:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.outsrcNdrDtlService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "退信明细（待确认信息清单）-通过id查询")
    @ApiOperation(value = "退信明细（待确认信息清单）-通过id查询", notes = "退信明细（待确认信息清单）-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<OutsrcNdrDtl> queryById(@RequestParam(name = "id", required = true) String id) {
        OutsrcNdrDtl outsrcNdrDtl = outsrcNdrDtlService.getById(id);
        if (outsrcNdrDtl == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(outsrcNdrDtl);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param outsrcNdrDtl
     */
    //@RequiresPermissions("com.boc.ewr:outsrc_ndr_dtl:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OutsrcNdrDtl outsrcNdrDtl) {
        return super.exportXls(request, outsrcNdrDtl, OutsrcNdrDtl.class, "退信明细（待确认信息清单）");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("outsrc_ndr_dtl:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OutsrcNdrDtl.class);
    }

    /**
     * 反馈，后台自动赋值反馈人，反馈时间
     *
     * @param outsrcNdrDtl
     * @return
     */
    @AutoLog(value = "待确认信息清单-反馈")
    @ApiOperation(value = "待确认信息清单-反馈", notes = "待确认信息清单-反馈")
    //@RequiresPermissions("com.boc.ewr:outsrc_ndr_dtl:edit")
    @RequestMapping(value = "/fdbk", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> fdbk(@RequestBody OutsrcNdrDtl outsrcNdrDtl) {
        OutsrcNdrDtl out = service.getById(outsrcNdrDtl.getId());

        //获取反馈人和反馈时间
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        out.setFdbkPsn(user.getRealname());
        out.setFdbkStatus(FdbkStatusEnum.NOAPPROVE.getIndex());
        out.setFdbkTime(new Date());
        service.updateById(out);
        return Result.OK("反馈成功!");
    }

    /**
     * 审核退回
     *
     * @param outsrcNdrDtl
     * @return
     */
    @AutoLog(value = "待确认信息清单-审核退回")
    @ApiOperation(value = "待确认信息清单-审核退回", notes = "待确认信息清单-审核退回")
    @RequestMapping(value = "/reject", method = {RequestMethod.PUT, RequestMethod.POST})
    //@RequiresPermissions("com.boc.ewr:outsrc_ndr_dtl:reject")
    public Result<String> reject(@RequestBody OutsrcNdrDtl outsrcNdrDtl) {
        OutsrcNdrDtl out = service.getById(outsrcNdrDtl.getId());
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        out.setCheckPsn(user.getRealname());
        out.setCheckTime(new Date());
        // 设置反馈状态为退回
        out.setFdbkStatus(FdbkStatusEnum.REJECT.getIndex());

        service.updateById(out);
        return Result.OK("退回成功!");
    }

    /**
     * 审核通过
     *
     * @param outsrcNdrDtl
     * @return
     */
    @AutoLog(value = "待确认信息清单-审核通过")
    @ApiOperation(value = "待确认信息清单-审核通过", notes = "待确认信息清单-审核通过")
    @RequestMapping(value = "/approve", method = {RequestMethod.PUT, RequestMethod.POST})
    //@RequiresPermissions("com.boc.ewr:outsrc_ndr_dtl:approve")
    public Result<String> approve(@RequestBody OutsrcNdrDtl outsrcNdrDtl) {
        OutsrcNdrDtl out = service.getById(outsrcNdrDtl.getId());

        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        out.setCheckPsn(user.getRealname());
        out.setCheckTime(new Date());
        // 审核通过才设置为反馈成功（审核通过）
        out.setFdbkStatus(FdbkStatusEnum.FEEDBACK.getIndex());
        service.updateById(out);
        return Result.OK("审核通过!");
    }

	private LoginUser getLoginUser() {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		return sysUser;
	}
}
